package com.example.csdn.controller;

import com.example.csdn.DTO.AddCategoryDTO;
import com.example.csdn.DTO.UpdateCategoryDTO;
import com.example.csdn.pojo.Category;
import com.example.csdn.pojo.Result;
import com.example.csdn.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/allCategory")
    public Result AllCategory(){
        List<Category> category = categoryService.AllCategory();
        return Result.success(category);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        Category category = categoryService.findById(id);
        return Result.success(category);
    }

    @PostMapping
    public Result add(@RequestBody @Validated AddCategoryDTO addCategoryDTO){
        categoryService.add(addCategoryDTO);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody @Validated UpdateCategoryDTO updateCategoryDTO){
        categoryService.update(updateCategoryDTO);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        categoryService.delete(id);
        return Result.success();
    }

}
